Operator Precedence
AppleScript allows you to combine expressions into larger, more complex expressions. When evaluating expressions, AppleScript uses operator precedence to determine which operations are performed first. Table 6-2 shows the order in which AppleScript performs operations.To see how operator precedence works, consider the following expression.
2 * 5 + 12 --result: 22To evaluate the expression, AppleScript performs the multiplication operation2 * 5
first, because as shown in Table 6-2, multiplication has higher precedence than addition.The column labeled "Associativity" in Table 6-2 indicates the order in which AppleScript performs operations if there are two or more operations of the same precedence in an expression. The word "none" in the Associativity column indicates that you cannot have multiple consecutive occurrences of the operation in an expression. For example, the expression
3 = 3 = 3
is not legal because the associativity for the equal operator (=) is "none." The word "unary" indicates that the operator is a unary operator. To evaluate expressions with multiple unary operators of the same order, AppleScript applies the operator closest to the operand first, then applies the next closest operator, and so on. For example, the expressionnot not not true
is evaluated asnot (not (not true))
.You can change the order in which AppleScript performs operations by grouping expressions in parentheses. As shown in Table 6-2, AppleScript evaluates expressions in parentheses first. For example, adding parentheses around
5
+ 12
in the following expression causes AppleScript to perform the addition operation first and changes the result.
2 * ( 5 + 12 ) --result:34